# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
Classes used to display help messages

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


import sys

# Project imports
from log import log

# GTK+ imports
import pygtk
if not sys.platform == 'win32':
    pygtk.require('2.0')
try:
    import gtk
except:
    log.error("gtk module not found!")
    sys.exit(1)

try:
    hasMozEmbed = True
    import gtkmozembed
except:
    log.warning("gtkmozembed module not found! Help will be disabled.")
    hasMozEmbed = False
    
log.debug("Loaded...")

if hasMozEmbed:
    class HelpViewer:
        "Display a browser with a help message."
        
        PATH = "file:///home/rrs/Mestrado/ITA/Codigo/src/help/"
        
        def __init__(self):
            
            # Create the browser widget
            # Set a temporary Mozilla profile (works around some bug)
            gtkmozembed.set_profile_path("/tmp", "simple_browser_user")
            self.window = None
            self.base = None
            self.buttonBar = None
            self.previous = None
            self.next = None
            self.browser = None
            
        def show(self, document, language = 'en_US'):
            "Shows the help window"
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    #        self.window.set_transient_for(parent)
            self.window.set_destroy_with_parent(True)
            self.window.set_modal(False)
            self.window.set_position(gtk.WIN_POS_CENTER)
            
            # Create the box for the buttons
            self.base = gtk.VBox()
            self.buttonBar = gtk.HButtonBox()
            self.buttonBar.set_layout(gtk.BUTTONBOX_START)
            
            self.previous = gtk.Button(label='', stock=gtk.STOCK_GO_BACK)
            self.next = gtk.Button(label='', stock=gtk.STOCK_GO_FORWARD)
            self.previous.connect('clicked', self.goPrevious)
            self.next.connect('clicked', self.goNext)
            
            
            self.buttonBar.pack_start(self.previous)
            self.buttonBar.pack_start(self.next)
            self.base.pack_start(self.buttonBar, expand = False, )
            self.window.add(self.base)
            
            self.browser = gtkmozembed.MozEmbed() # Create the browser widget
    
            # Set-up the browser widget before we display it
            self.base.pack_start(self.browser)
            self.browser.load_url(HelpViewer.PATH + document + ".xhtml") # Load page
            
            # Attempt to set the size of the browser widget to 600x400 pixels
            self.browser.set_size_request(450, 250)
            
            # Adds a callback so we now when we can read the title
            self.browser.connect('title', self.titleChanged)
            
            # Try to show the browser widget before we show the window, so that the 
            # window appears at the correct size (600x400)
            self.browser.show()
            
            self.window.show_all()
                
        def goPrevious(self, widget = None):
            "Called when the previous button was clicked"
            self.browser.go_back()
            
        def goNext(self, widget = None):
            "Called when the next button was clicked"
            self.browser.go_forward()
            
        def titleChanged(self, widget = None):
            "Called when the title has been changed"
            self.window.set_title(self.browser.get_title())
            self.previous.set_sensitive(self.browser.can_go_back())
            self.next.set_sensitive(self.browser.can_go_forward())
else:
    class HelpViewer:
        "No help available."
        
        def show(self, document, language = 'en_US'):
            "Shows a 'sorry' window"
            dlg = gtk.MessageDialog(type=gtk.MESSAGE_INFO,
                                    buttons=gtk.BUTTONS_NONE,
                                    message_format='Sorry. No help installed.')
            dlg.show_all()

